package org.linlinjava.litemall.admin.service.order_manage;

import cn.hutool.core.io.FileUtil;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.dao.LitemallStorageMapper;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.util.OssUtil;
import org.linlinjava.litemall.db.util.SnowFlake;
import org.linlinjava.litemall.db.util.SystemUtil;
import org.linlinjava.litemall.db.util.UploadConstant;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wj
 * @date 2021/8/4 17:49
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AgedmallUploadService {

    @Resource
    private LitemallStorageMapper litemallStorageMapper;


    public Object uploadFile(MultipartFile file, String path,Integer fileType){
        if(file.isEmpty()){
            return ResponseUtil.fail(505,"上传文件不能为空");
        }
        else{
            String fileOriName=file.getOriginalFilename();
            String fileSuffix= fileOriName.substring(fileOriName.lastIndexOf("."));
            String fileName= fileOriName.substring(0, fileOriName.lastIndexOf("."))+"-"+ SnowFlake.getIds();
            String realPath="";
            String relativePath="";
            String folderPath="";
            String ossUrl=null;
            if(StringUtils.isNotBlank(path)){
                if(path.substring(0,1).equals("/")){
                    path=path.substring(1,path.length());
                }
                folderPath=path;
                relativePath="/upload/"+path+fileName+fileSuffix;
                path=path+fileName+fileSuffix;
            }
            else{
                path=fileName+fileSuffix;
                relativePath="/upload/"+fileName+fileSuffix;
            }
            if(SystemUtil.isWindows()){
                realPath=UploadConstant.WINDOS_BASE_UPLOAD_URL+path;
                folderPath= UploadConstant.WINDOS_BASE_UPLOAD_URL+folderPath;
            }
            else if(SystemUtil.isMacOS()||SystemUtil.isMacOSX()){
                realPath=UploadConstant.MAC_BASE_UPLOAD_URL+path;
                folderPath=UploadConstant.MAC_BASE_UPLOAD_URL+folderPath;
            }
            else{
                realPath=UploadConstant.LINUX_BASE_UPLOAD_URL+path;
                folderPath=UploadConstant.LINUX_BASE_UPLOAD_URL+folderPath;
            }
            try {
                FileUtil.mkdir(new File(folderPath));
                file.transferTo(new File(realPath));
                OssUtil ossUtil=new OssUtil();
                //TODO 上传到oss
                ossUrl=ossUtil.uploadFile(realPath,path);
               if(StringUtils.isBlank(ossUrl)){
                   return ResponseUtil.fail(3000,"oss保存失败");
               }
                FileUtil.del(new File(realPath));

            } catch (IOException e) {
                return ResponseUtil.fail(506,"保存文件失败");
            }
            LitemallStorage litemallStorage=new LitemallStorage();
            litemallStorage.setFileType(fileType);
            litemallStorage.setName(fileName);
            litemallStorage.setSuffix(fileSuffix);
            litemallStorage.setSize(file.getSize());
            litemallStorage.setRealFileUrl(ossUrl);
            litemallStorage.setUrl(relativePath);
            litemallStorage.setAddTime(LocalDateTime.now());
            litemallStorageMapper.insertSelective(litemallStorage);
            Map<String,String> map=new HashMap<>();
//            map.put("relativePath",relativePath);
            //TODO 后续使用ossUrl
            map.put("relativePath",ossUrl);
            map.put("fileId",litemallStorage.getId().toString());
        return ResponseUtil.ok(map);
        }
    }
    
    
    public Object deleteFile(String id){
        LitemallStorage litemallStorage = litemallStorageMapper.selectByPrimaryKey(Integer.parseInt(id));
        FileUtil.del(FileUtil.newFile(litemallStorage.getRealFileUrl()));
        litemallStorage.setDeleted(true);
        litemallStorageMapper.updateByPrimaryKeySelective(litemallStorage);
        return ResponseUtil.ok();
    }
    
}
